<template lang="pug">
data-page(
  v-bind="pageParams"
)
  h3(style="text-align:center") 申请项目统计
  h4(style="text-align:center") 统计时间：（2024-09-30 00:00:00~2024-10-29 23:59:59） 
  mt-table(
    v-bind="tableProps"
  )
</template>

<script setup lang="ts">
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type TableProps,
  useNotify,
  type Action,
  Icon,
} from "@scmt/common-components";
import { useQuery } from "@scmt/common-hooks";
import {
  type ApiBasedOptions,
  type QueryPagination,
  useApi,
} from "@scmt/core-api";
import {
  type SpecimenEntity,
  type SpecimenQueryParams,
} from "@scmt/api-lis-specimen";

const $api = useApi();
const $notify = useNotify();
const $utils = useUtils();
const queryPramas = reactive<SpecimenQueryParams>({
  state: 0,
  startDate: $utils.date().startOf("date").format("YYYY-MM-DD HH:mm:ss"),
  endDate: $utils.date().endOf("date").format("YYYY-MM-DD HH:mm:ss"),
  pageNum: 1,
  pageSize: 20,
});

const editingObj = ref<null | SpecimenEntity>(null);

const selectEquipment = defineModel();
const selectionRow = ref([]);
import { useUtils } from "@scmt/lib-utils";

const { query, data, loadingMap } = useQuery({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.lis.specimen.query(params);
  },
  save: (model: SpecimenEntity) => {
    return $api.lis.specimen.save(model).then(() => {
      $notify.success("保存成功");
      editingObj.value = null;

      query("tableData", {
        pageSize: 1,
      });
    });
  },
  destroy: (ids: string) => {
    return $api.lis.specimen.destroy(ids).then(() => {
      $notify.success("删除成功");

      query("tableData", {
        pageSize: 1,
      });
    });
  },
});

onMounted(() => {
  query("tableData");
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "inspectionTime",
      label: "检验日期",
      as: "date",
      options: {
        type: "datetimerange",
      },
    },
    {
      prop: "inspectionTime",
      label: "患者来源",
      as: "select",
      options: {
        loadMethod: () => $api.dict.item.listAsOptions("patient-type"),
      },
    },
    {
      prop: "inspectionTime",
      label: "开单科室",
      as: "select",
      options: {
        loadMethod: () => $api.upms.org.listAsOptions(),
      },
    },
    {
      prop: "inspectionTime",
      label: "开单医生",
      as: "select",
      options: {
        loadMethod: () => $api.user.listAllDoctorAsOptions({ officeId: "" }),
      },
    },
    {
      prop: "inspectionTime",
      label: "申请项目名称",
    },
    {
      prop: "inspectionTime",
      label: "标本类型",
      as: "select",
      options: {
        loadMethod: () => $api.lis.specimen.category.listAsOptions(),
      },
    },
    {
      prop: "inspectionTime",
      label: "仪器名称",
      as: "select",
      options: {
        loadMethod: () => $api.lis.instrument.listAsOptions(),
      },
    },
  ];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    onQuery: () => query("tableData"),
    onAct: onPageActions,
    flex: true,
  };

  return dataPageProps;
});

const tableData = [
  {
    source: "合计",
    billingDepartment: "",
    billingDoctor: "",
    applicationProject: "",
    specimen: "",
    device: "",
    frequency: "0",
    unitPrice: "/",
    money: "0.00",
  },
  {
    source: "来源",
    billingDepartment: "开单科室",
    billingDoctor: "开单医生",
    applicationProject: "申请项目",
    specimen: "标本",
    device: "仪器",
    frequency: "次数",
    unitPrice: "单价",
    money: "金额",
  },
  {
    source: "",
    billingDepartment: "",
    billingDoctor: "",
    applicationProject: "",
    specimen: "",
    device: "",
    frequency: "",
    unitPrice: "",
    money: "",
  },
];

const tableProps = computed(() => {
  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData,
    insertIndex: false,
    spanMethod: arraySpanMethod,
    columns: [
      {
        prop: "source",
      },
      {
        prop: "billingDepartment",
      },
      {
        prop: "billingDoctor",
      },
      {
        prop: "applicationProject",
      },
      {
        prop: "specimen",
      },
      {
        prop: "device",
      },
      {
        prop: "frequency",
      },
      {
        prop: "unitPrice",
      },
      {
        prop: "money",
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) => query("tableData", pagination),
    onRowClick: (row: any) => {
      selectEquipment.value = row;
    },
    onSelectionChange: (selection: any) => {
      selectionRow.value = selection;
    },
    highlightCurrentRow: true,
    showHeader: false,
  };
  return tableProps;
});

const arraySpanMethod = ({
  row,
  column,
  rowIndex,
  columnIndex,
}: SpanMethodProps) => {
  if (rowIndex === 0) {
    if (columnIndex === 0) {
      return [1, 6];
    } else if (columnIndex < 6) {
      return [0, 0];
    }
  }
};

const onPageActions = (action: IAction, model?: any) => {};
</script>

<style lang="less" scoped></style>
